package com.xc.service.impl;

import com.github.pagehelper.StringUtil;
import com.xc.common.ServerResponse;
import com.xc.dao.AccessMapper;
import com.xc.dao.RealTimeMapper;
import com.xc.dao.RoleMapper;
import com.xc.pojo.Access;
import com.xc.pojo.FundsApply;
import com.xc.pojo.RealTime;
import com.xc.pojo.Role;
import com.xc.service.RealTimeService;
import com.xc.service.RoleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    AccessMapper accessMapper;


    @Override
    public ServerResponse insertRole(String description, String name, String purview) throws Exception {
        Integer id = roleMapper.selectByName(name);
        if (null != id){
            return ServerResponse.createByErrorMsg("已存在角色！");
        }
        roleMapper.insertRole(description, name);
        Integer rodeId = roleMapper.selectByName(name);
        List<String> purviewList = Arrays.asList(purview.split(","));
        if (purviewList.size() != 0) {
            purviewList.forEach(str -> {
                accessMapper.insertAccess(str, rodeId);
            });
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse updateRole(String purview, Integer roleId, Integer status, String description, String name) {
        roleMapper.updateRole(roleId, status, description, name);
        List<String> purviewList = Arrays.asList(purview.split(","));
        List<Access> accessesList = accessMapper.selectByRoleId(roleId);
        purviewList.forEach(str -> {
            List<Access> accesses = accessesList.stream().filter(access -> access.getPurviewval().equals(str)).collect(Collectors.toList());
            if (accesses.size() == 0) {
                accessMapper.insertAccess(str, roleId);
            }
        });
        accessesList.forEach(str -> {
            List<String> collect = purviewList.stream().filter(pur -> pur.equals(str.getPurviewval())).collect(Collectors.toList());
            if (collect.size() == 0) {
                accessMapper.deleteAccess(str.getPurviewval(), roleId);
            }
        });
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse deleteRole(Integer roleId) {
        roleMapper.deleteRole(roleId);
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse bandingRole(Integer userId, Integer roleId) {
        roleMapper.bandingRole(userId,roleId);
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse listByRole(Integer roleId) {
        return ServerResponse.createBySuccess(accessMapper.byRoleId(roleId));
    }

    @Override
    public ServerResponse roleList() {
        return ServerResponse.createBySuccess(accessMapper.roleList());
    }

}
